﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.Common;
using System.Text.RegularExpressions;
namespace SLLottery.Controls
{
    public partial class NumPanel : UserControl
    {
        Dictionary<CheckBox, bool> dicChk;
        TextBox textBox;
        public System.Text.StringBuilder sbTxt;
        public NumPanel()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(NumPanel_Loaded);
            dicChk = new Dictionary<CheckBox, bool>();
            sbTxt = new System.Text.StringBuilder();
        }
        public string Text
        {
            get;
            set;
        }
        public bool isWriten = false;
        public bool isCreated = false;
        public NumPanel(int colNum, int rowNum)
            : this()
        {
            ColNum = colNum;
            RowNum = rowNum;
        }
        public int ColNum = 10;
        public int RowNum = 1;

        void NumPanel_Loaded(object sender, RoutedEventArgs e)
        {
            //if(!isCreated)
            //CreateElement();
        }
        public List<NumButton> GetNumButton()
        {
            List<NumButton> list = new List<NumButton>();
            if (isWriten)
                return list;
            foreach (var item in NumContainer.Children)
            {
                if (item is BetNum)
                {
                    list.AddRange(((BetNum)item).GetNumButton());
                }
            }
            return list;
        }
        public TextBox GetNumText()
        {
            foreach (var elm in NumContainer.Children)
            {
                if (elm.GetType() == typeof(TextBox))
                    return (TextBox)elm;
            }
            return null;
        }
        public List<Button> GetCmdButton()
        {
            List<Button> list = new List<Button>();
            foreach (BetNum bn in NumContainer.Children)
            {
                list.AddRange(bn.GetCmdButton());
            }
            return list;
        }
        public void CreateElement()
        {
            MainPage.dicWs.Clear();
            dicChk.Clear();
            if (MainPage.playTypeRadio == 48 || MainPage.playTypeRadio == 49 || MainPage.playTypeRadio == 50 || MainPage.playTypeRadio == 51 || MainPage.playTypeRadio == 53 || MainPage.playTypeRadio == 54)
            {
                StackPanel CheckBoxPanel = new StackPanel();
                CheckBoxPanel.Orientation = Orientation.Horizontal;
                CheckBoxPanel.Height = 20;
                CheckBoxPanel.Margin = new Thickness(0, 5, 0, 0);
                CheckBox chboxw = new CheckBox() { Content = "万W", Foreground = new SolidColorBrush(Colors.White) };
                chboxw.Checked += new RoutedEventHandler(chbox_Checked);
                chboxw.Unchecked += new RoutedEventHandler(chbox_Unchecked);
                chboxw.Margin = new Thickness(5, 0, 0, 0);
                chboxw.Tag = 0;
                chboxw.IsChecked = true;
                CheckBoxPanel.Children.Add(chboxw);
                dicChk.Add(chboxw, chboxw.IsChecked.Value);
                CheckBox chboxq = new CheckBox() { Content = "千Q", Foreground = new SolidColorBrush(Colors.White) };
                chboxq.Checked += new RoutedEventHandler(chbox_Checked);
                chboxq.Unchecked += new RoutedEventHandler(chbox_Unchecked);
                chboxq.Margin = new Thickness(20, 0, 0, 0);
                chboxq.Tag = 1;
                chboxq.IsChecked = true;
                CheckBoxPanel.Children.Add(chboxq);
                dicChk.Add(chboxq, chboxq.IsChecked.Value);
                CheckBox chboxb = new CheckBox() { Content = "百B", Foreground = new SolidColorBrush(Colors.White) };
                chboxb.Checked += new RoutedEventHandler(chbox_Checked);
                chboxb.Unchecked += new RoutedEventHandler(chbox_Unchecked);
                chboxb.Margin = new Thickness(20, 0, 0, 0);
                chboxb.Tag = 2;
                if (MainPage.playType == 21 || MainPage.playType == 22)
                {
                    chboxb.IsChecked = true;
                }
                else
                {
                    chboxb.IsEnabled = false;
                }
                CheckBoxPanel.Children.Add(chboxb);
                dicChk.Add(chboxb, chboxb.IsChecked.Value);
                CheckBox chboxs = new CheckBox() { Content = "十S", Foreground = new SolidColorBrush(Colors.White) };
                chboxs.Checked += new RoutedEventHandler(chbox_Checked);
                chboxs.Unchecked += new RoutedEventHandler(chbox_Unchecked);
                chboxs.Margin = new Thickness(20, 0, 0, 0);
                chboxs.Tag = 3;
                CheckBoxPanel.Children.Add(chboxs);
                dicChk.Add(chboxs, chboxs.IsChecked.Value);
                chboxs.IsEnabled = false;
                CheckBox chboxg = new CheckBox() { Content = "个G", Foreground = new SolidColorBrush(Colors.White) };
                chboxg.Checked += new RoutedEventHandler(chbox_Checked);
                chboxg.Unchecked += new RoutedEventHandler(chbox_Unchecked);
                chboxg.Margin = new Thickness(20, 0, 0, 0);
                chboxg.Tag = 4;
                CheckBoxPanel.Children.Add(chboxg);
                dicChk.Add(chboxg, chboxg.IsChecked.Value);
                chboxg.IsEnabled = false;
                TextBlock txtQ = new TextBlock() { Foreground = new SolidColorBrush(Colors.White), Cursor = Cursors.Hand };
                txtQ.Text = (MainPage.playType == 21 || MainPage.playType == 22) ? "前三" : "前二";
                txtQ.Margin = new Thickness(20, 0, 0, 0);
                txtQ.MouseLeftButtonDown += new MouseButtonEventHandler(txtQ_MouseLeftButtonDown);
                CheckBoxPanel.Children.Add(txtQ);
                TextBlock txtZ = new TextBlock() { Foreground = new SolidColorBrush(Colors.White), Cursor = Cursors.Hand };
                txtZ.Text = (MainPage.playType == 21 || MainPage.playType == 22) ? "中三" : "中二";
                txtZ.Margin = new Thickness(20, 0, 0, 0);
                txtZ.MouseLeftButtonDown += new MouseButtonEventHandler(txtZ_MouseLeftButtonDown);
                CheckBoxPanel.Children.Add(txtZ);
                TextBlock txtH = new TextBlock() { Foreground = new SolidColorBrush(Colors.White), Cursor = Cursors.Hand };
                txtH.Text = (MainPage.playType == 21 || MainPage.playType == 22) ? "后三" : "后二";
                txtH.Margin = new Thickness(20, 0, 0, 0);
                txtH.MouseLeftButtonDown += new MouseButtonEventHandler(txtH_MouseLeftButtonDown);
                CheckBoxPanel.Children.Add(txtH);
                this.NumContainer.Children.Add(CheckBoxPanel);
            }

            if (!isWriten)
            {
                string[] array = Text.Split(',');
                foreach (string str in array)
                {
                    BetNum bet = new BetNum(ColNum, RowNum);
                    bet.TitleText = str;
                    bet.CreateElement();
                    this.NumContainer.Children.Add(bet);
                }
            }
            else
            {
                textBox = new TextBox();
                textBox.TextWrapping = TextWrapping.Wrap;
                textBox.AcceptsReturn = true;
                textBox.Width = 595;
                textBox.Height = 75;
                textBox.Name = "TextBox_Num";
                tbTitle.Text = "输入号码";
                textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
                this.NumContainer.Children.Add(textBox);
            }
            isCreated = true;
        }

        private void chbox_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            MainPage.dicWs.Remove(chk.Tag.ToString());
            foreach (var item in dicChk.Keys)
            {
                item.IsEnabled = true;
            }
        }

        private void chbox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            MainPage.dicWs.Add(chk.Tag.ToString(), int.Parse(chk.Tag.ToString()));
            if (MainPage.playType == 21 || MainPage.playType == 22)
            {
                if (dicChk.Where(p => p.Key.IsChecked == true).Count() == 3)
                {
                    foreach (var item in dicChk.Where(p => p.Key.IsChecked == false))
                    {
                        item.Key.IsEnabled = false;
                    }
                }
                else
                {
                    foreach (var item in dicChk.Keys)
                    {
                        item.IsEnabled = true;
                    }
                }
            }
            else if (MainPage.playType == 23 || MainPage.playType == 24)
            {
                if (dicChk.Where(p => p.Key.IsChecked == true).Count() == 2)
                {
                    foreach (var item in dicChk.Where(p => p.Key.IsChecked == false))
                    {
                        item.Key.IsEnabled = false;
                    }
                }
                else
                {
                    foreach (var item in dicChk.Keys)
                    {
                        item.IsEnabled = true;
                    }
                }
            }
        }

        private void txtH_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainPage.dicWs.Clear();
            StackPanel stk = (sender as TextBlock).Parent as StackPanel;
            for (int i = 0; i < 5; i++)
            {
                (stk.Children[i] as CheckBox).IsChecked = false;
                (stk.Children[i] as CheckBox).IsEnabled = true;
            }
            if (MainPage.playType == 21 || MainPage.playType == 22)
            {
                for (int i = 2; i < 5; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
            else if (MainPage.playType == 23 || MainPage.playType == 24)
            {
                for (int i = 3; i < 5; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
        }

        private void txtZ_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainPage.dicWs.Clear();
            StackPanel stk = (sender as TextBlock).Parent as StackPanel;
            for (int i = 0; i < 5; i++)
            {
                (stk.Children[i] as CheckBox).IsChecked = false;
                (stk.Children[i] as CheckBox).IsEnabled = true;
            }
            if (MainPage.playType == 21 || MainPage.playType == 22)
            {
                for (int i = 1; i < 4; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
            else if (MainPage.playType == 23 || MainPage.playType == 24)
            {
                for (int i = 2; i < 4; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
        }

        private void txtQ_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainPage.dicWs.Clear();
            StackPanel stk = (sender as TextBlock).Parent as StackPanel;
            for (int i = 0; i < 5; i++)
            {
                (stk.Children[i] as CheckBox).IsChecked = false;
                (stk.Children[i] as CheckBox).IsEnabled = true;
            }
            if (MainPage.playType == 21 || MainPage.playType == 22)
            {
                for (int i = 0; i < 3; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
            else if (MainPage.playType == 23 || MainPage.playType == 24)
            {
                for (int i = 0; i < 2; i++)
                {
                    (stk.Children[i] as CheckBox).IsChecked = true;
                }
            }
        }

        void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (!((e.Key >= Key.D0 && e.Key <= Key.D9)
                || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Space)
                )
            {
                e.Handled = true;
            }
            if (e.Key == Key.Enter)
            {
                (sender as TextBox).Text = (sender as TextBox).Text + "\r";
                (sender as TextBox).Select((sender as TextBox).Text.Length, 0);
                e.Handled = true;
            }
        }
        public void ClearChild()
        {
            NumContainer.Children.Clear();
        }
        public static bool IsNumeric(string Value)
        {
            string pattern = @"^[0-9]*$";
            Match m = Regex.Match(Value, pattern);
            return m.Success;
        }
        public string GetChoosedNum(SSCRXPlayType type)
        {
            string num = string.Empty;
            sbTxt.Clear();
            if (NumContainer.Children.Count > 1 && NumContainer.Children[1].GetType() == typeof(TextBox))
            {
                TextBox tb = (TextBox)NumContainer.Children[1];
                if (tb.Text.Trim() == "")
                {
                    return string.Empty;
                }
                string[] nums = tb.Text.TrimEnd().TrimStart().Replace("\r", " ").Replace("　", " ").Replace("\t", " ").Split(' ');
                foreach (string n in nums)
                {
                    n.Replace(" ", "");
                    if (n == string.Empty)
                        continue;
                    if (!IsNumeric(n))
                    {
                        return "@含有未知数字!";
                    }
                    switch (type)
                    {
                        case SSCRXPlayType._R3Zhix:
                            if (n.Length != 3)
                            {
                                return "@每注号码为3个数字!";
                            }
                            if (MainPage.playTypeRadio==48&&MainPage.dicWs.Count != 3)
                            {
                                return "@（万千百十个）中请至少选择3个位置的号码!";
                            }
                            break;
                        case SSCRXPlayType._R3Zux:
                            if (n.Length != 3)
                            {
                                return "@每注号码为3个数字!";
                            }
                            if (MainPage.dicWs.Count != 3)
                            {
                                return "@（万千百十个）中请至少选择3个位置的号码!";
                            }
                            break;
                        case SSCRXPlayType._R2Zhix:
                            if (n.Length != 2)
                            {
                                return "@每注号码为2个数字!";
                            }
                            if (MainPage.playTypeRadio == 53 && MainPage.dicWs.Count != 2)
                            {
                                return "@（万千百十个）中请至少选择2个位置的号码!";
                            }
                            break;
                        case SSCRXPlayType._R2Zux:
                            if (n.Length != 2)
                            {
                                return "@每注号码为2个数字!";
                            }
                            if (MainPage.dicWs.Count != 2)
                            {
                                return "@（万千百十个）中请至少选择2个位置的号码!";
                            }
                            break;
                    }
                    if (MainPage.playTypeRadio!=51)
                    {
                        char[] tempchr = "     ".ToArray();
                        char[] nchr = n.ToArray();
                        int j = 0;
                        foreach (var item in MainPage.dicWs.OrderBy(p => p.Value))
                        {
                            tempchr[item.Value] = nchr.ElementAt(j);
                            j++;
                        }
                        num += string.Join(",", tempchr) + "$";
                    }
                    else
                    {
                        num += (n + "$");
                    }
                }
                foreach (var item in dicChk.Keys)
                {
                    if (item.IsChecked == true)
                    {
                        sbTxt.Append(item.Content.ToString().Substring(0, 1) + ",");
                    }
                }
                return num;
            }
            int wsNum = 0;
            foreach (var item in NumContainer.Children)
            {
                if (item is BetNum)
                {
                    if (!string.IsNullOrEmpty((item as BetNum).GetChoosedNum()))
                    {
                        wsNum++;
                    }
                    num += (item as BetNum).GetChoosedNum() + ",";
                }
            }
            switch (MainPage.playTypeRadio)
            {
                case 47:
                    if (wsNum != 3)
                    {
                        return "@（万千百十个）中请至少选择3个位置的号码!";
                    }
                    break;
                case 48:
                    if (wsNum != 3)
                    {
                        return "@（万千百十个）中请至少选择3个位置的号码!";
                    }
                    if (MainPage.dicWs.Count != 3)
                    {
                        return "@（万千百十个）中请至少选择3个位置的号码!";
                    }
                    break;
                case 49:
                    if (num.Replace(",", "").Trim().Length < 2)
                    {
                        return "@请选择至少2个号码!";
                    }
                    if (MainPage.dicWs.Count != 3)
                    {
                        return "@（万千百十个）中请至少选择3个位置的号码!";
                    }
                    break;
                case 50:
                    if (num.Replace(",", "").Trim().Length < 3)
                    {
                        return "@请选择至少3个号码!";
                    }
                    if (MainPage.dicWs.Count != 3)
                    {
                        return "@（万千百十个）中请至少选择3个位置的号码!";
                    }
                    break;
                case 52:
                    if (wsNum != 2)
                    {
                        return "@（万千百十个）中请至少选择2个位置的号码!";
                    }
                    break;
                case 54:
                    if (num.Replace(",", "").Trim().Length < 2)
                    {
                        return "@请选择至少2个号码!";
                    }
                    if (MainPage.dicWs.Count != 2)
                    {
                        return "@（万千百十个）中请至少选择2个位置的号码!";
                    }
                    break;
            }
            if (num.Replace(",", "").Trim().Length == 0)
            {
                foreach (var item in dicChk.Keys)
                {
                    if (item.IsChecked == true)
                    {
                        sbTxt.Append(item.Content.ToString().Substring(0, 1) + ",");
                    }
                }
                return string.Empty;
            }
            else
            {
                foreach (var item in dicChk.Keys)
                {
                    if (item.IsChecked == true)
                    {
                        sbTxt.Append(item.Content.ToString().Substring(0, 1) + ",");
                    }
                }
                return num.Substring(0, num.Length - 1);
            }
        }
        public string GetChoosedNum(SSCPlayType type)
        {
            string num = string.Empty;
            if (NumContainer.Children[0].GetType() == typeof(TextBox))
            {
                TextBox tb = (TextBox)NumContainer.Children[0];
                if (tb.Text.Trim() == "")
                    return string.Empty;
                string[] nums = tb.Text.TrimEnd().TrimStart().Replace("\r", " ").Replace("　", " ").Replace("\t", " ").Split(' ');
                foreach (string n in nums)
                {
                    n.Replace(" ", "");
                    if (n == string.Empty)
                        continue;
                    if (!IsNumeric(n))
                        return "@含有未知数字!";
                    switch (type)
                    {
                        case SSCPlayType._3xZhix:
                            if (n.Length != 3)
                                return "@每注号码为3个数字!";
                            break;
                        case SSCPlayType._3xZux:
                            if (n.Length != 3)
                                return "@每注号码为3个数字!";
                            break;
                        case SSCPlayType._2xZhix:
                            if (n.Length != 2)
                                return "@每注号码为2个数字!";
                            break;
                        case SSCPlayType._2xZux:
                            if (n.Length != 2)
                                return "@每注号码为2个数字!";
                            break;
                    }
                    num += (n + " ");
                }
                return num.TrimEnd();
            }
            foreach (BetNum bn in NumContainer.Children)
            {
                switch (type)
                {
                    case SSCPlayType._3xZhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case SSCPlayType._3xZux:
                        if (bn.GetChoosedNum().Length < 3)
                            return string.Empty;
                        break;
                    case SSCPlayType._2xZhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case SSCPlayType._2xZux:
                        if (bn.GetChoosedNum().Length < 2)
                            return string.Empty;
                        break;
                    case SSCPlayType._H1Zhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case SSCPlayType._Bddw:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case SSCPlayType._Ddw:
                        break;
                }
                num += bn.GetChoosedNum() + ",";
            }
            if (num.Replace(",", "").Trim().Length == 0)
                return string.Empty;
            return num.Substring(0, num.Length - 1);
        }
        public string GetChoosedNum(_3DPlayType type)
        {
            string num = string.Empty;
            if (NumContainer.Children[0].GetType() == typeof(TextBox))
            {
                TextBox tb = (TextBox)NumContainer.Children[0];
                if (tb.Text.Trim() == "")
                    return string.Empty;
                string[] nums = tb.Text.TrimEnd().TrimStart().Replace("\r", " ").Replace("　", " ").Replace("\t", " ").Split(' ');
                foreach (string n in nums)
                {
                    n.Replace(" ", "");
                    if (n == string.Empty)
                        continue;
                    if (!IsNumeric(n))
                        return "@含有未知数字!";
                    switch (type)
                    {
                        case _3DPlayType._3xZhix:
                            if (n.Length != 3)
                                return "@每注号码为3个数字!";
                            break;
                        case _3DPlayType._3xZux:
                            if (n.Length != 3)
                                return "@每注号码为3个数字!";
                            break;
                        case _3DPlayType._2xZhix:
                            if (n.Length != 2)
                                return "@每注号码为2个数字!";
                            break;
                        case _3DPlayType._2xZux:
                            if (n.Length != 2)
                                return "@每注号码为2个数字!";
                            break;
                    }
                    num += (n + " ");
                }
                return num.TrimEnd();
            }
            foreach (BetNum bn in NumContainer.Children)
            {
                switch (type)
                {
                    case _3DPlayType._3xZhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case _3DPlayType._3xZux:
                        if (bn.GetChoosedNum().Length < 3)
                            return string.Empty;
                        break;
                    case _3DPlayType._2xZhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case _3DPlayType._2xZux:
                        if (bn.GetChoosedNum().Length < 2)
                            return string.Empty;
                        break;
                    case _3DPlayType._H1Zhix:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case _3DPlayType._Bddw:
                        if (bn.GetChoosedNum().Length < 1)
                            return string.Empty;
                        break;
                    case _3DPlayType._Ddw:
                        break;
                }
                num += bn.GetChoosedNum() + ",";
            }
            if (num.Replace(",", "").Trim().Length == 0)
                return string.Empty;
            return num.Substring(0, num.Length - 1);
        }
        public string GetChoosedNum(SSQPlayType type)
        {
            string num = string.Empty;
            foreach (BetNum bn in NumContainer.Children)
            {
                num += bn.GetChoosedNum() + ",";
            }
            if (num.Replace(",", "").Trim().Length == 0)
                return string.Empty;
            return num.Substring(0, num.Length - 1);
        }
        public string GetChoosedNum()
        {
            string num = string.Empty;
            foreach (BetNum bn in NumContainer.Children)
            {
                num += bn.GetChoosedNum() + ",";
            }
            if (num.Replace(",", "").Trim().Length == 0)
                return string.Empty;
            return num.Substring(0, num.Length - 1);
        }
        public void ClearAllNums()
        {
            if (isWriten)
            {
                TextBox tb;
                if (NumContainer.Children[0].GetType() == typeof(TextBox))
                {
                    tb = (TextBox)NumContainer.Children[0];
                }
                else
                {
                    tb = (TextBox)NumContainer.Children[1];
                }
                tb.Text = string.Empty;
                return;
            }
            foreach (var item in NumContainer.Children)
            {
                if (item is BetNum)
                {
                    (item as BetNum).ClearNumStatus();
                }
            }
        }
    }
}
